package com.demo;

import com.demo.util.CamelUtils;
import freemarker.cache.FileTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 实体 输入dto生成
 * @author gy
 * @date 2023/8/24
 */
public class EntityOutputDTOGenerator35 {

      private static final String tableName = "t_hardware_server_manage";
//    private static final String tableName = "t_resource_monitor_alert_manage";
//    private static final String tableName = "t_sample_manage";
//    private static final String tableName = "t_evaluation_schedule";
//    private static final String tableName = "t_analysis_task_manage";
//    private static final String tableName = "t_information_database_property_manage";

    private static final String outputPackagePath="D:\\generator\\src\\main\\java\\com\\sinoits\\" +
            "microservicehlyy\\dto\\output\\";
    private static final String entityPackagePath="D:\\generator\\src\\main\\java\\com\\sinoits\\" +
            "microservicehlyy\\test\\entity\\";

    private static final String driver="com.mysql.cj.jdbc.Driver";

        private static final String ipAddrAndPort="192.168.6.35:23306";
//    private static final String ipAddrAndPort="127.0.0.1:3308";

    private static final String dbCatalog = "hlyy";

        private static final String username="sinoits";
//    private static final String username="root";

        private static final String password="sinoits@123";
//    private static final String password="123456";

    private static final String serverUrl ="jdbc:mysql://"+ipAddrAndPort+"?" +
            "useSSL=false&useUnicode=true&characterEncoding=utf8&autoReconnect=true" +
            "&rewriteBatchedStatements=TRUE&serverTimezone=Asia/Shanghai&allowMultiQueries=true" +
            "&useInformationSchema=true";//没有指定具体哪个数据库，现在获取的是整个连接
    private static final String url="jdbc:mysql://"+ipAddrAndPort+"/"+dbCatalog+"?" +
            "useSSL=false&useUnicode=true&characterEncoding=utf8&autoReconnect=true" +
            "&rewriteBatchedStatements=TRUE&serverTimezone=Asia/Shanghai&allowMultiQueries=true" +
            "&useInformationSchema=true";//指定hlyy数据库

    // 表名前缀
    private static final String tableNamePrefix = "t_";
    // 实体名称
    private static final String entityName =
            CamelUtils.line2upperCamel(tableName.replace(tableNamePrefix,""));

    private static final String author = "gy";
    private static final String entityOutputDTOSuffix="OutputDTO";
    private static final String fileType = ".java";

    @Test
    public void generatorOutputDTO() throws Exception {
        long startTime = System.currentTimeMillis();

        //1，创建FreeMarker的配置类
        Configuration cfg=new Configuration();
        //2，指定模板加载器，将模板加入缓存中
        //文件路径加载器，获取到templates文件的路径
        String templates = this.getClass().getClassLoader().getResource("templates").getPath();
        FileTemplateLoader fileTemplateLoader=new FileTemplateLoader(new File(templates));
        cfg.setTemplateLoader(fileTemplateLoader);
        //3，获取模板
        Template template = cfg.getTemplate("EntityOutputDTO.ftl");

        //4，构造数据模型
        Map<String, Object> map = buildModel();

        //5，文件输出
        /**
         * 处理模型
         *      参数一 数据模型
         *      参数二 writer对象（FileWriter（文件输出），printWriter（控制台输出））
         */
        System.out.println("-----------------生成 EntityOutputDTO 开始-----------------------");

        template.process(map,new FileWriter(
                new File(outputPackagePath+entityName+entityOutputDTOSuffix+fileType)));

        System.out.println("***EntityOutputDTO文件位置："+outputPackagePath+entityName+entityOutputDTOSuffix+fileType);
        System.out.println("-----------------生成 EntityOutputDTO 结束-----------------------");
        long endTime = System.currentTimeMillis();
        System.out.println("执行耗时["+(endTime - startTime)+"]毫秒");
    }

    /**
     * 构造数据模型
     * @return
     */
    private Map<String, Object> buildModel() throws Exception {

        //获取连接
        Class.forName(driver);//注册驱动
        Connection connection = DriverManager.getConnection(url, username,password);

        //获取元数据
        DatabaseMetaData metaData = connection.getMetaData();
        //获取数据库中表信息（mysql可以这样写，oracle会有一点区别）
        //参数1：当前操作的数据库 参数2：mysql可为空，oracle填写用户名（要大写） 参数3：null是查询所有表 非空是查询目标表 参数4：类型 table是表，view是视图
        ResultSet resultSet = metaData.getTables(dbCatalog, null, tableName, new String[]{"TABLE"});

        String TABLE_NAME = null;
        String REMARKS = null;
        while (resultSet.next()){
            //会打印出该数据库下的所有表名
            String TABLE_CAT = resultSet.getString("TABLE_CAT");
            String TABLE_SCHEM = resultSet.getString("TABLE_SCHEM");
            TABLE_NAME = resultSet.getString("TABLE_NAME");
            String TABLE_TYPE = resultSet.getString("TABLE_TYPE");
            REMARKS = resultSet.getString("REMARKS");
            System.out.printf("表类别:%s, 表模式:%s, 表名称:%s, 表类型:%s, 表描述:%s\n",
                    TABLE_CAT, TABLE_SCHEM, TABLE_NAME, TABLE_TYPE,REMARKS);
        }
        resultSet.close();
        connection.close();

        Map<String,Object> map=new HashMap<String, Object>();
        map.put("tableName",TABLE_NAME);
        map.put("entity",entityName);
        map.put("tableComment",REMARKS);

        String createDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        map.put("createDate",createDate);
        map.put("author",author);

        return map;
    }

}